<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\video\datacache;
use think\facade\Cache;

/**
 * 视频评论 缓存层
 * @package app\user_center\model
 */
class VideoPlayComment{

    /**
     * 获取评论单条数据
     * @param int $play_id 视频id
     * @param int $page 页码（默认1）
     * @author jason wang <120907037@qq.com>
     * @return bool
     */
    public function getDataByCommentid($user_uid, $commentid){
        $info = model('video/VideoPlayComment') -> getDataByCommentid($commentid, $cache = true);
        if($info['code'] != 200) return array_out(-1001, 'error');
        $value = $info['data'];

        //用户信息
        $userinfo =controller('user_center/Userlogic', 'logic\user') -> get_user_info($user_uid, $value -> uid);

        //是否点赞
        $support = model('VideoPlayCommentSupport') -> isSupport($user_uid, $value -> id);//是否点赞
        $isSupport = $support['code'] == 200 ? 1 : 0;
                
        $result = [
            'commentId'		        => (int)$value -> id,
            'commentContent'		=> (string)$value -> content,
            'commentReplyTimes' 	=> (int)$value -> reply_times,
            'commentReplyNum' 		=> (int)$value -> reply_times,
            'commentSupportTimes' 	=> (int)$value -> support_times,
            'commentSupportNum' 	=> (int)$value -> support_times,
            'commentDatetime' 		=> (int)$value -> create_time,
            'isSupport' 			=> (int)$isSupport,
            'userInfo'              => $userinfo['data'],
        ];

        
        return array_out(200, 'success', (object)$result);
        
    }
	
	/**
     * 视频评论
     * @param play_id int 视频id
     * @param cache 是否更新 缓存
     * @author shiqiang
     * @return array
     */
    public function getPlayCommentList($play_id, $cache = false){
        $cacheName = 'play_comment_list_'.$play_id;
		$list = Cache::get($cacheName);
        if(!$list || $cache){//查出视频下所有评论
			$video_info = model('video/VideoPlay') -> getPlayFatherPlayInfo($play_id);
			$play_id = $video_info['data'];//关联的视频id
			
            $list = model('video/VideoPlayComment') -> where('play_id', $play_id) -> order('support_times desc,reply_times desc,create_time desc') -> order('id desc') -> select() -> toArray();
            if(!empty($list)){
                Cache::set($cacheName, $list);
            }
        }
		
		return array_out(200, 'success', $list);
    }

}
